package comnet.ftpclient;


import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;

import javax.swing.*;
import javax.swing.border.TitledBorder;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.*;
import java.io.IOException;

public class Connect extends JDialog {
    private JPanel panel1;
    private JTextField hostAddressTextField;
    private JTextField usernameTextField;
    private JPasswordField passwordPasswordField;
    private JRadioButton normalRadioButton;
    private JRadioButton anonymousRadioButton;
    private JButton connectButton;
    private JButton cancelButton;
    private JTextField portTextField;
    private FtpClient client;

    public Connect(FtpClient client) {
        this.client = client;
        add(panel1);

        getRootPane().setDefaultButton(connectButton);

        setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                onCancel();
            }
        });

        panel1.registerKeyboardAction(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                onCancel();
            }
        }, KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);

        panel1.registerKeyboardAction(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                onOK();
            }
        }, KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);

        connectButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                onOK();
            }
        });

        cancelButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                onCancel();
            }
        });

        normalRadioButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if (!usernameTextField.isEditable()) {
                    usernameTextField.setText("");
                    passwordPasswordField.setToolTipText(null);
                    usernameTextField.setEditable(true);
                }
            }
        });

        anonymousRadioButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                usernameTextField.setText("anonymous");
                usernameTextField.setEditable(false);
                passwordPasswordField.setToolTipText("Enter your email-address, e.g. mail@host.com");
            }
        });

        setModalityType(ModalityType.APPLICATION_MODAL);
        setResizable(false);
        pack();
        setVisible(true);
    }

    private void onOK() {
        String host = hostAddressTextField.getText();
        int port = 0;
        try {
            port = Integer.parseInt(portTextField.getText());
        } catch (NumberFormatException e) {
            return;
        }
        String username = usernameTextField.getText();
        String password = new String(passwordPasswordField.getPassword());
        client.setHost(host);
        client.setPort(port);
        client.setUsername(username);
        client.setPassword(password);
        try {
            if (client.setConnection()) {
                dispose();
            } else {
                JOptionPane.showMessageDialog(this, "Could not connect:\n" + client.getLastServerMessage());
            }
        } catch (IOException e) {
            JOptionPane.showMessageDialog(this, "Could not connect:\n" + e.getMessage());
        }
    }

    private void onCancel() {
        dispose();
    }

    {
// GUI initializer generated by IntelliJ IDEA GUI Designer
// >>> IMPORTANT!! <<<
// DO NOT EDIT OR ADD ANY CODE HERE!
        $$$setupUI$$$();
    }

    /**
     * Method generated by IntelliJ IDEA GUI Designer >>> IMPORTANT!! <<< DO NOT
     * edit this method OR call it in your code!
     */
    private void $$$setupUI$$$() {
        panel1 = new JPanel();
        panel1.setLayout(new FormLayout("fill:82px:noGrow,left:4dlu:noGrow,fill:82px:noGrow,left:42px:noGrow,left:28dlu:noGrow,left:1dlu:noGrow,fill:46px:grow", "center:25px:noGrow,top:15dlu:noGrow,center:max(d;4px):noGrow,center:28px:noGrow,top:9dlu:noGrow,center:28px:noGrow,center:max(d;4px):noGrow,top:7dlu:noGrow,center:max(d;4px):noGrow"));
        panel1.setVisible(true);
        final JLabel label1 = new JLabel();
        label1.setText("Host address:");
        label1.setDisplayedMnemonic('H');
        label1.setDisplayedMnemonicIndex(0);
        CellConstraints cc = new CellConstraints();
        panel1.add(label1, cc.xyw(1, 1, 4));
        hostAddressTextField = new JTextField();
        hostAddressTextField.setText("mazzer.nu");
        panel1.add(hostAddressTextField, cc.xyw(1, 2, 5, CellConstraints.FILL, CellConstraints.DEFAULT));
        usernameTextField = new JTextField();
        usernameTextField.setText("");
        panel1.add(usernameTextField, cc.xyw(1, 4, 4, CellConstraints.FILL, CellConstraints.TOP));
        final JLabel label2 = new JLabel();
        label2.setText("Username:");
        label2.setDisplayedMnemonic('U');
        label2.setDisplayedMnemonicIndex(0);
        panel1.add(label2, cc.xyw(1, 3, 4));
        final JLabel label3 = new JLabel();
        label3.setText("Password:");
        label3.setDisplayedMnemonic('P');
        label3.setDisplayedMnemonicIndex(0);
        panel1.add(label3, cc.xyw(1, 5, 4));
        passwordPasswordField = new JPasswordField();
        passwordPasswordField.setText("");
        panel1.add(passwordPasswordField, cc.xyw(1, 6, 4, CellConstraints.FILL, CellConstraints.TOP));
        final JPanel panel2 = new JPanel();
        panel2.setLayout(new GridBagLayout());
        panel1.add(panel2, cc.xywh(5, 3, 3, 4, CellConstraints.CENTER, CellConstraints.FILL));
        panel2.setBorder(BorderFactory.createTitledBorder(null, "Login method", TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.TOP));
        anonymousRadioButton = new JRadioButton();
        anonymousRadioButton.setSelected(false);
        anonymousRadioButton.setText("Anonymous");
        anonymousRadioButton.setMnemonic('A');
        anonymousRadioButton.setDisplayedMnemonicIndex(0);
        anonymousRadioButton.setToolTipText("Anonymous connection");
        GridBagConstraints gbc;
        gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.anchor = GridBagConstraints.WEST;
        panel2.add(anonymousRadioButton, gbc);
        normalRadioButton = new JRadioButton();
        normalRadioButton.setSelected(true);
        normalRadioButton.setText("Normal");
        normalRadioButton.setMnemonic('N');
        normalRadioButton.setDisplayedMnemonicIndex(0);
        normalRadioButton.setToolTipText("Normal connection");
        gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.anchor = GridBagConstraints.WEST;
        panel2.add(normalRadioButton, gbc);
        portTextField = new JTextField();
        portTextField.setText("3014");
        panel1.add(portTextField, cc.xy(7, 2, CellConstraints.FILL, CellConstraints.DEFAULT));
        final JLabel label4 = new JLabel();
        label4.setText("Port");
        label4.setDisplayedMnemonic('O');
        label4.setDisplayedMnemonicIndex(1);
        panel1.add(label4, cc.xy(7, 1));
        connectButton = new JButton();
        connectButton.setText("Connect");
        connectButton.setMnemonic('E');
        connectButton.setDisplayedMnemonicIndex(4);
        panel1.add(connectButton, cc.xy(1, 7, CellConstraints.DEFAULT, CellConstraints.FILL));
        cancelButton = new JButton();
        cancelButton.setText("Cancel");
        panel1.add(cancelButton, cc.xy(3, 7, CellConstraints.DEFAULT, CellConstraints.FILL));
        label1.setLabelFor(hostAddressTextField);
        label2.setLabelFor(usernameTextField);
        label3.setLabelFor(passwordPasswordField);
        label4.setLabelFor(portTextField);
        ButtonGroup buttonGroup;
        buttonGroup = new ButtonGroup();
        buttonGroup.add(normalRadioButton);
        buttonGroup.add(anonymousRadioButton);
    }

    /** @noinspection ALL */
    public JComponent $$$getRootComponent$$$() {
        return panel1;
    }
}
